// 虚拟网盘API服务
import { apiRequest } from './utils';
import { AuthService } from '../../services/authService';

export interface VirtualFile {
  id: number;
  name: string;
  isFolder: boolean;
  parentId: number | null;
  size?: number;
  createdAt: string;
  updatedAt: string;
}

export interface FileListResponse {
  items: VirtualFile[];
  pagination: {
    page: number;
    pageSize: number;
    total: number;
    totalPages: number;
  };
}

export class DriveApiService {
  /**
   * 获取文件列表
   * @param parentId 父文件夹ID，null表示根目录
   * @param foldersOnly 是否只返回文件夹
   * @param page 页码
   * @param pageSize 每页大小
   */
  static async listFiles(
    parentId: number | null = null,
    foldersOnly: boolean = false,
    page: number = 1,
    pageSize: number = 20
  ): Promise<any> {
    const params = new URLSearchParams({
      page: page.toString(),
      pageSize: pageSize.toString(),
      foldersOnly: foldersOnly.toString()
    });

    if (parentId !== null) {
      params.append('parentId', parentId.toString());
    }

    const response = await fetch(`/api/netdisk/files?${params}`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        ...AuthService.getAuthHeaders()
      },
    });

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    return response.json();
  }

  /**
   * 获取文件夹路径
   * @param folderId 文件夹ID
   */
  static async getFolderPath(folderId: number): Promise<string> {
    // 这里可以实现获取文件夹完整路径的逻辑
    // 目前简化处理，返回文件夹名称
    try {
      const response = await fetch(`/api/netdisk/files/${folderId}/path`, {
        method: 'GET',
        headers: apiRequest.getJsonHeaders()
      });

      if (!response.ok) {
        throw new Error('获取文件夹路径失败');
      }

      const data = await response.json();
      return data.data.path || '/';
    } catch (error) {
      // 如果API不存在，返回默认路径
      return '/';
    }
  }
}

export default DriveApiService;